package com.xiaobai.common.utils.result;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.map.MapUtil;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.math.NumberUtils;

import java.util.Collection;
import java.util.Map;
import java.util.Objects;

public class AssertUtils {
    public AssertUtils() {
    }

    public static <E extends Throwable> void isTrue(boolean expression, ResultCode resultCode) throws E {
        if (!expression) {
            throw new ServiceException(resultCode);
        }
    }

    public static <E extends Throwable> void isTrue(boolean expression, ResultCode resultCode, String message) throws E {
        if (!expression) {
            throw new ServiceException(resultCode, message);
        }
    }

    public static <E extends Throwable> void isFalse(boolean expression, ResultCode resultCode) throws E {
        if (expression) {
            throw new ServiceException(resultCode);
        }
    }

    public static <E extends Throwable> void isFalse(boolean expression, ResultCode resultCode, String message) throws E {
        if (expression) {
            throw new ServiceException(resultCode, message);
        }
    }

    public static <E extends Throwable> void isNull(Object obj, ResultCode resultCode) throws E {
        if (Objects.nonNull(obj)) {
            throw new ServiceException(resultCode);
        }
    }

    public static <E extends Throwable> void isNull(Object obj, ResultCode resultCode, String message) throws E {
        if (Objects.nonNull(obj)) {
            throw new ServiceException(resultCode, message);
        }
    }

    public static <E extends Throwable> void isNotNull(Object obj, ResultCode resultCode, String message) throws E {
        if (Objects.isNull(obj)) {
            throw new ServiceException(resultCode, message);
        }
    }

    public static <E extends Throwable> void isNotNull(Object obj, ResultCode resultCode) throws E {
        if (Objects.isNull(obj)) {
            throw new ServiceException(resultCode);
        }
    }

    public static <E extends Throwable> void isEmpty(Collection<?> collections, ResultCode resultCode, String message) throws E {
        if (CollectionUtil.isNotEmpty(collections)) {
            throw new ServiceException(resultCode, message);
        }
    }

    public static <E extends Throwable> void isEmpty(Collection<?> collections, ResultCode resultCode) throws E {
        if (CollectionUtil.isNotEmpty(collections)) {
            throw new ServiceException(resultCode);
        }
    }

    public static <E extends Throwable> void isNotEmpty(Collection<?> collections, ResultCode resultCode) throws E {
        if (CollectionUtil.isEmpty(collections)) {
            throw new ServiceException(resultCode);
        }
    }

    public static <E extends Throwable> void isNotEmpty(Collection<?> collections, ResultCode resultCode, String message) throws E {
        if (CollectionUtil.isEmpty(collections)) {
            throw new ServiceException(resultCode, message);
        }
    }

    public static <E extends Throwable> void isEmpty(Map<?, ?> map, ResultCode resultCode) throws E {
        if (MapUtil.isNotEmpty(map)) {
            throw new ServiceException(resultCode);
        }
    }

    public static <E extends Throwable> void isEmpty(Map<?, ?> map, ResultCode resultCode, String message) throws E {
        if (MapUtil.isNotEmpty(map)) {
            throw new ServiceException(resultCode, message);
        }
    }

    public static <E extends Throwable> void isNotEmpty(Map<?, ?> map, ResultCode resultCode) throws E {
        if (MapUtil.isEmpty(map)) {
            throw new ServiceException(resultCode);
        }
    }

    public static <E extends Throwable> void isNotEmpty(Map<?, ?> map, ResultCode resultCode, String message) throws E {
        if (MapUtil.isEmpty(map)) {
            throw new ServiceException(resultCode, message);
        }
    }

    public static <E extends Throwable> void isBlank(String str, ResultCode resultCode) throws E {
        if (StringUtils.isNotBlank(str)) {
            throw new ServiceException(resultCode);
        }
    }

    public static <E extends Throwable> void isBlank(String str, ResultCode resultCode, String message) throws E {
        if (StringUtils.isNotBlank(str)) {
            throw new ServiceException(resultCode, message);
        }
    }

    public static <E extends Throwable> void isNotBlank(String str, ResultCode resultCode) throws E {
        if (StringUtils.isBlank(str)) {
            throw new ServiceException(resultCode);
        }
    }

    public static <E extends Throwable> void isNotBlank(String str, ResultCode resultCode, String message) throws E {
        if (StringUtils.isBlank(str)) {
            throw new ServiceException(resultCode, message);
        }
    }

    public static <E extends Throwable> void eqZero(Number number, ResultCode resultCode, String message) throws E {
        if (NumberUtils.compare((Integer) number, 0) != 0) {
            throw new ServiceException(resultCode, message);
        }
    }

    public static <E extends Throwable> void eqZero(Number number, ResultCode resultCode) throws E {
        if (NumberUtils.compare((Integer) number, 0) != 0) {
            throw new ServiceException(resultCode);
        }
    }

    public static <E extends Throwable> void lessThanZero(Number number, ResultCode resultCode) throws E {
        if (NumberUtils.compare((Integer) number, 0) > 0) {
            throw new ServiceException(resultCode);
        }
    }

    public static <E extends Throwable> void lessThanZero(Number number, ResultCode resultCode, String message) throws E {
        if (NumberUtils.compare((Integer) number, 0) > 0) {
            throw new ServiceException(resultCode, message);
        }
    }

    public static <E extends Throwable> void gatherThanZero(Number number, ResultCode resultCode) throws E {
        if (NumberUtils.compare((Integer) number, 0) < 0) {
            throw new ServiceException(resultCode);
        }
    }

    public static <E extends Throwable> void gatherThanZero(Number number, ResultCode resultCode, String message) throws E {
        if (NumberUtils.compare((Integer) number, 0) < 0) {
            throw new ServiceException(resultCode, message);
        }
    }
}